// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../public/public'),
		clientApi = require('../public/clientapi'),
		clientStorage = require('../public/storage');

	var ZqxyModule = require('./zqxy/init');
	var ZrzxModule = require('./zrzx/init');
	var ZrrmModule = require('./zrrm/init');
	var HgtcjqsModule = require('./hgtcjqs/init');
	var SgtcjqsModule = require('./sgtcjqs/init');
	var ZrhsqsModule = require('./zrhsqs/init');

	var ZrzjqsModule = require('./zrzjqs/init');
	var ZrcjqsModule = require('./zrcjqs/init');
	var ZrgzfModule = require('./zrgzf/init');
	var QrpjModule = require('./qrpj/init');
	var ZdtjModule = require('./zdtj/init');
	var RzrqModule = require('./rzrq/init');

	var TableModule = require('./table/init');
	var HsgtzjModule = require('./hsgtzj/init');

	var WorldModule = require('./world/init');
	var MarketModule = require('./market/init');

	var overallModule = require('./overall/init');


	var dataStore = require('../control/datastore');

	var renderDefault = {
		customFile: 'custommodule.log',
		init: function () {
			this.events();
			this.getDefault();
		},
		events: function () {
			// 模块拖拽事件
			$('#modules-list-ul').dragsort({
				dragSelector: 'li',
				dragSelectorExclude: '.module-main, .date-wrap, i, a',
				dragBetween: false,
				scrollContainer: '#modules-list',
				dragEnd: function() {

				},
				placeHolderTemplate: '<li><div class="module-item placeholder"></div></li>'
			});

			$('#modules-list-ul').on('click', '.hc-link', function () {
				var link = $(this).attr('href');
				clientApi.thsUtil.openURL(link, 4);
				return false;
			});
		},
		getDefault: function() {
			var self = this;
			var customModule = clientStorage.getFileContent(self.customFile);
			if (customModule == false) {
				self.renderDefault(self.defaultModule);
				self.customModule = self.defaultModule;
			} else {
				self.customModule = customModule;
				self.renderDefault(self.customModule);
			}
		},
		customModule: [],
		delCustomModule: function (mname) {
			var self = this;
			$.each(self.customModule, function (i, val) {
				if (val.type === mname) {
					self.customModule.splice(i, 1);
					return false;
				}
			});
			self.saveCustomModule();
		},
		delCustomModuleT: function (mname, cond) {
			var self = this;
			$.each(self.customModule, function (i, val) {
				if (val.condition == cond) {
					self.customModule.splice(i, 1);
					return false;
				}
			});
			self.saveCustomModule();
		},
		saveCustomModule: function () {
			var self = this;
			clientStorage.writeFileContent(self.customFile, JSON.stringify(self.customModule));
		},
		defaultModule: [{
			type: 'overall'
		}, {
			type: 'market'
		}, {
			type: 'world'
		}, {
			type: 'zdtj'
		}, {
			type: 'qrpj'
		}, {
			type: 'zqxy'
		}, {
			type: 'hsgtzj'
		}, {
			type: 'rzrq'
		}, {
			type: 'zrzx'
		}, {
			type: 'zrrm'
		}],
		// defaultModule: [{
		// 	type: 'table',
		// 	condition: '股价小于10'
		// }],
		// modulesList: {
		// 	zqxyModule: null,
		// 	zrzxModule: null,
		// 	zrrmModule: null,

		// 	hgtcjqsModule: null,
		// 	sgtcjqsModule: null,

		// 	zrhsqsModule: null,
		// 	zrzjqsModule: null,
		// 	zrcjqsModule: null,
		// 	zrgzfModule: null,

		// 	qrpjModule: null,
		// 	zdtjModule: null,
		// 	rzrqModule: null,
		// 	hsgtzjModule: null,
		// 	worldModule: null,

		// 	marketModule: null,

		// 	overallModule: null
		// },
		tableId: 1,
		renderDefault: function(modulesList) {
			var self = this;
			$.each(modulesList, function(i, val) {
				var type = val.type;
				switch (type) {
					case 'zqxy':
						dataStore.modulesList['zqxyModule'] = new ZqxyModule();
						break;
					case 'zrzx':
						dataStore.modulesList['zrzxModule'] = new ZrzxModule();
						break;
					case 'zrrm':
						dataStore.modulesList['zrrmModule'] = new ZrrmModule();
						break;

					case 'hgtcjqs':
						dataStore.modulesList['hgtcjqsModule'] = new HgtcjqsModule();
						break;

					case 'sgtcjqs':
						dataStore.modulesList['sgtcjqsModule'] = new SgtcjqsModule();
						break;

					case 'zrhsqs':
						dataStore.modulesList['zrhsqsModule'] = new ZrhsqsModule();
						break;
					case 'zrzjqs':
						dataStore.modulesList['zrzjqsModule'] = new ZrzjqsModule();
						break;
					case 'zrcjqs':
						dataStore.modulesList['zrcjqsModule'] = new ZrcjqsModule();
						break;
					case 'zrgzf':
						dataStore.modulesList['zrgzfModule'] = new ZrgzfModule();
						break;

					case 'qrpj':
						dataStore.modulesList['qrpjModule'] = new QrpjModule();
						break;
					case 'zdtj':
						dataStore.modulesList['zdtjModule'] = new ZdtjModule();
						break;
					case 'rzrq':
						dataStore.modulesList['rzrqModule'] = new RzrqModule();
						break;

					case 'hsgtzj':
						dataStore.modulesList['hsgtzjModule'] = new HsgtzjModule();
						break;

					case 'world':
						dataStore.modulesList['worldModule'] = new WorldModule({});
						break;

					case 'market':
						dataStore.modulesList['marketModule'] = new MarketModule();
						break;

					case 'overall':
						dataStore.modulesList['overallModule'] = overallModule;
						overallModule.init();
						dataStore.isHasOverall = true;
						break;

					case 'table':
						var options = {
							condition: val.condition,
							tableId: self.tableId++
						};
						var tableModule = new TableModule(options);
						break;
					default:
						break;
						// return false;
				}
			});

			self.autoRefresh();
		},
		addSystemItem: function (params) {
			var self = this;
			var type = params.type;
			switch (type) {
				case 'zqxy':
					dataStore.modulesList['zqxyModule'] = new ZqxyModule();
					break;
				case 'zrzx':
					dataStore.modulesList['zrzxModule'] = new ZrzxModule();
					break;
				case 'zrrm':
					dataStore.modulesList['zrrmModule'] = new ZrrmModule();
					break;

				case 'hgtcjqs':
					dataStore.modulesList['hgtcjqsModule'] = new HgtcjqsModule();
					break;

				case 'sgtcjqs':
					dataStore.modulesList['sgtcjqsModule'] = new SgtcjqsModule();
					break;

				case 'zrhsqs':
					dataStore.modulesList['zrhsqsModule'] = new ZrhsqsModule();
					break;
				case 'zrzjqs':
					dataStore.modulesList['zrzjqsModule'] = new ZrzjqsModule();
					break;
				case 'zrcjqs':
					dataStore.modulesList['zrcjqsModule'] = new ZrcjqsModule();
					break;
				case 'zrgzf':
					dataStore.modulesList['zrgzfModule'] = new ZrgzfModule();
					break;

				case 'qrpj':
					dataStore.modulesList['qrpjModule'] = new QrpjModule();
					break;
				case 'zdtj':
					dataStore.modulesList['zdtjModule'] = new ZdtjModule();
					break;
				case 'rzrq':
					dataStore.modulesList['rzrqModule'] = new RzrqModule();
					break;

				case 'hsgtzj':
					dataStore.modulesList['hsgtzjModule'] = new HsgtzjModule();
					break;

				case 'world':
					dataStore.modulesList['worldModule'] = new WorldModule({});
					break;

				case 'market':
					dataStore.modulesList['marketModule'] = new MarketModule();
					break;

				case 'overall':
					dataStore.modulesList['overallModule'] = overallModule;
					overallModule.init();
					dataStore.isHasOverall = true;
					break;


				case 'table':
					var options = {
						condition: params.condition,
						tableId: self.tableId++
					};
					var tableModule = new TableModule(options);
					break;
				default:
					break;
					// return false;
			}

			self.saveCustomModule();
		},

		autoRefresh: function () {
			var self = this;
			setInterval(function () {
				// overallModule.refresh();
				var isHasOverall = dataStore.isHasOverall;
				var tradeDay = clientApi.getTradingDay();

				// $('#robot-switch').append('1****')
				$.each(dataStore.modulesList, function (i, moduleItem) {
					if (!moduleItem) {
						// $('#robot-switch').append('false');
						return true;
					}
					// $('#robot-switch').append('true');
					if (i == 'overallModule') {
						moduleItem.refresh(tradeDay);
					} else {
						moduleItem.updateSeries(isHasOverall, tradeDay);
					}
				});
			}, 30000);
		}

	};

	module.exports = renderDefault;
});
